package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.li.interfaces.OnItemClickListener;
import com.li.widget.CircleImageView;
import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.ShopInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/25 0025
 * Description: 选择商铺
 */

public class ChooseShopAdapter extends RecyclerView.Adapter<ChooseShopAdapter.ChooseShopHolder> {


    private List<ShopInfo> mShopInfoList;
    private LayoutInflater mInflater;
    private Context mContext;

    public ChooseShopAdapter(Context context) {
        mContext=context;
        mInflater = LayoutInflater.from(context);
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public OnItemClickListener mItemClickListener;


    @Override
    public ChooseShopHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_city_shop, parent, false);

        return new ChooseShopHolder(view);
    }

    @Override
    public void onBindViewHolder(ChooseShopHolder holder, int position) {
        ShopInfo shopInfo=mShopInfoList.get(position);
        final int pos=position;
        RequestOptions options=new RequestOptions();
        options.error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher);
        Glide.with(mContext).load(shopInfo.getTitlepic()).apply(options).into(holder.mImgHeader);
        holder.mTxtShopName.setTxtRight(shopInfo.getName());
        holder.mTxtShopPhone.setTxtRight(shopInfo.getMobile());
        if (shopInfo.getIs_one().equals("0")) {
            holder.mTxtDeviceNum.setTxtLeft("未分配首装");
            holder.mTxtDeviceNum.setLeftTextColor(Color.parseColor("#3ea9ed"));
            if (mItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClickListener.onItemClick(v,pos);
                    }
                });
            }
        }else {
            holder.mTxtDeviceNum.setTxtLeft("已分配首装");
            holder.mTxtDeviceNum.setLeftTextColor(Color.BLACK);
        }
    }

    @Override
    public int getItemCount() {
        return mShopInfoList == null ? 0 : mShopInfoList.size();
    }

    public void setShopInfoList(List<ShopInfo> shopInfoList) {
        mShopInfoList = shopInfoList;
        notifyDataSetChanged();
    }

    static class ChooseShopHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_header)
        CircleImageView mImgHeader;
        @BindView(R.id.txt_shop_name)
        DoubleTextView mTxtShopName;
        @BindView(R.id.txt_shop_phone)
        DoubleTextView mTxtShopPhone;
        @BindView(R.id.txt_device_num)
        DoubleTextView mTxtDeviceNum;
        public ChooseShopHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
